package com.hspedu.reflection;

import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/22
 * @time 11:23
 */
public class Reflection01 {
    public static void main(String[] args) throws Exception{

        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\workspace_idea\\java0\\hspedu\\chapter23\\src\\re.properties"));
        String classfullpath = properties.get("classfullpath").toString();
        String methodName = properties.get("method").toString();

        Class cls = Class.forName(classfullpath);

        Object o = cls.newInstance();
        System.out.println("o的运行类型=" + o.getClass());

        Method method1 = cls.getMethod(methodName);
        System.out.println("=============================");
        method1.invoke(o);

        //getField不能得到私有的属性
        Field nameField = cls.getField("age");
        System.out.println(nameField.get(o));

        //()中可以指带构造器参数类型，不写就是无参构造器
        Constructor constructor = cls.getConstructor();
        System.out.println("constructor = " + constructor);
        Object o1 = constructor.newInstance();
        System.out.println("o1.getClass() = " + o1.getClass());
    }
}
